﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Net.Sockets;


namespace linq编程
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            list.Add(1);
            list.AddRange(new int[] { 3, 4, 5, 6, 7, 8 });
            Console.WriteLine(list);

            var query1 = from val in list select val;

            foreach (int variable in query1)
            {
                Console.WriteLine("{0} ",variable);   
            }

            Student[] stAry =
            {
                new Student("张三", 20, "广州"),
                new Student("李四", 22, "上海"),
                new Student("王五", 25, "北京"),
                new Student("马六", 26, "广州"),
                new Student("赵器", 27, "武汉"),

            };


            var query2 = 
                from vall in stAry
                orderby vall.Age descending 
                where vall.Addr.Equals("广州") 
                      // && vall.Age > 20
                select vall.Name;

            foreach (var student in query2)
            {
                Console.WriteLine(student);
            }

            Console.WriteLine("=========================================");

            var query3 =
                    from vall in stAry
                    orderby vall.Addr descending 
                    group vall by vall.Addr;

            foreach (var grp in query3)
            {
                Console.WriteLine(grp.Key);

                foreach (var student in grp)
                {
                    Console.WriteLine("{0}",student.Name);
                }

                Console.WriteLine("---");
            }
            Console.ReadKey();
        }
    }

    class Student
    {

        private string name;
        private int age;
        private string addr;

        public Student(string name, int age, string addr)
        {
            this.name = name;
            this.age = age;
            this.addr = addr;
        }

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
            }
        }

        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }

        public string Addr
        {
            get
            {
                return addr;
            }
            set
            {
                addr = value;
            }
        }
    }
}
